import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import {
  getRoleName,
  getRoleTypeName,
  ROLE_OPERATE,
  ROLE_FINANCE,
  ROLE_DATA_STATISTICS,
  ROLE_PACKAGE_MANAGEMENT,
  ROLE_INVENTORY_MANAGEMENT,
  ROLE_ACCOUNT_MANAGEMENT,
  ROLE_CARGO_MANAGEMENT,
  ROLE_WORKORDER_MANAGEMENT
} from '@/utils/user';
import FullPage from "@/layout/components/FullPage.vue";
/* Router Modules */

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/login/register'),
    name: 'register',
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/adminLogin',
    component: () => import('@/views/login/adminLogin'),
    name: 'adminLogin',
    hidden: true,
  },
  {
    path: '/backLogin',
    component: () => import('@/views/login/backLogin'),
    name: 'backLogin',
    hidden: true,
  },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/',
    redirect: '/index',
    children: []
  },
  {
    path: '/index',
    component: () => import('@/views/index/index'),
    name: 'index',
    // 首页
    meta: { title: 'home', icon: 'el-icon-c-scale-to-original',},
    hidden: true
  },
  // 数据统计
  {
    path: '/statistics',
    component: Layout,
    redirect: '/statistics/orderList',
    // 数据统计
    meta: {title:'dataStatistics', icon: 'circle'},
    children: [
      {
        path: '/statistics/stickerList',
        component: () => import('@/views/statistics/stickerList'),
        name: 'stickerList',
        // 贴单数据
        meta: { title: 'labelingDataV2',   icon: ''},
      },
      {
        path: '/statistics/orderList',
        component: () => import('@/views/statistics/orderList'),
        name: 'orderList',
        // 订单数据
        meta: { title: 'orderDataV2',  icon: ''},
      },
      {
        path: '/order/storageList',
        component: () => import('@/views/statistics/storageList'),
        name: 'storageList',
        // 仓储数据
        meta: { title: 'warehouseData',   icon: ''},
      },
      {
        path: '/statistics/sheinImgList',
        component: () => import('@/views/statistics/sheinImgList'),
        name: 'sheinImgList',
        // 希音图片
        meta: { title: 'sheinImgList',   icon: ''},
      },
    ]
  },
  // 小包管理
  {
    path: '/packetOrders',
    component: Layout,
    redirect: '/packetOrders/packetOrderList',
    meta: {title:'packetOrders',roles:['admin',ROLE_OPERATE,ROLE_PACKAGE_MANAGEMENT], icon: 'circle'},
    children: [
      // 订单列表
      {
        path: '/packetOrders/packetOrderList',
        component: () => import('@/views/packetOrders/packetOrderList'),
        name: 'packetOrderList',
        meta: { title: 'packetOrderList',  roles:['admin',ROLE_OPERATE,ROLE_PACKAGE_MANAGEMENT], icon: ''},
      },
      // 订单详情
      {
        path: '/packetOrders/packetOrderInfo',
        component: () => import('@/views/packetOrders/packetOrderInfo'),
        name: 'packetOrderInfo',
        meta: { title: 'packetOrderInfo',  roles:['admin',ROLE_OPERATE,ROLE_PACKAGE_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      // 添加或者编辑订单
      {
        path: '/packetOrders/updatePacketOrder',
        component: () => import('@/views/packetOrders/updatePacketOrder'),
        name: 'updatePacketOrder',
        meta: { title: 'updatePacketOrder',  roles:['admin',ROLE_OPERATE,ROLE_PACKAGE_MANAGEMENT], icon: '', noCache: true},
      },
      // 小包批量上传查看错误信息页面
      {
        path: '/packetOrders/batchUploadPacketErrorData',
        component: () => import('@/views/packetOrders/batchUploadPacketErrorData'),
        name: 'batchUploadPacketErrorData',
        meta: { title: 'batchUploadPacketErrorData', roles:['admin',ROLE_OPERATE,ROLE_PACKAGE_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
    ]
  },
  //帮助中心
  {
    path: '/help',
    component: FullPage, // 使用新的布局组件
    hidden: true,
    redirect: '/help/helppage',
    meta: {title:'help'},
    children: [
      {
        name: 'HelpPage',
        path: '/help/helppage',
        component: () => import('@/views/help-document/index.vue'),
        meta: { title: '帮助中心'},
        hidden: true,
      }
    ]
  },
  {
    path: '/shipment',
    component: Layout,
    redirect: '/shipment/confirmShipmentList',
    // 货件管理
    meta: {title:'shipmentManagement',roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: 'circle'},
    children: [
      {
        path: '/shipment/addShippingPlan',
        component: () => import('@/views/shippingPlan/addShippingPlan'),
        name: 'addShippingPlan',
        // 添加货件
        meta: { title: '添加发货', roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: '', noCache: true},
      },
      {
        path: '/shipment/shippingPlanInfo',
        component: () => import('@/views/shippingPlan/shippingPlanInfo'),
        name: 'shippingPlanInfo',
        // 添加货件
        meta: { title: '计划详情', roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/shipment/shippingPlanList',
        component: () => import('@/views/shippingPlan/shippingPlanList'),
        name: 'shippingPlanList',
        // 添加货件
        meta: { title: '发货计划', roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: ''},
      },
      {
        path: '/shipment/addShipment',
        component: () => import('@/views/shipment/addShipment'),
        name: 'addShipment',
        // 添加货件
        meta: { title: 'addShipment', roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/shipment/shipmentInfo',
        component: () => import('@/views/shipment/shipmentInfo'),
        name: 'shipmentInfo',
        // 货件详情
        meta: { title: 'shipmentDetails', roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/shipment/shipmentListMoveWarehouse',
        component: () => import('@/views/shipment/mwShipmentList'),
        name: 'shipmentListMoveWarehouse',
        meta: { title: '移仓货件', roles: ['admin',ROLE_OPERATE,ROLE_CARGO_MANAGEMENT], icon: ''},
      },
    ]
  },
  // 库存管理
  {
    path: '/stock',
    component: Layout,
    redirect: '/stock/stockList',
    // 库存管理
    meta: {title:'inventoryManagement',roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: 'circle'},
    children: [
      {
        path: '/stock/stockList',
        component: () => import('@/views/stock/stockList'),
        name: 'stockList',
        // 库存列表
        meta: { title: 'inventoryList',  roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: ''},
      },
      {
        path: '/stock/stockInfo',
        component: () => import('@/views/stock/stockInfo'),
        name: 'stockInfo',
        // 库存列表
        meta: { title: '库存明细',  roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/stock/addMiddle',
        component: () => import('@/views/middleWork/addMiddle'),
        name: 'addMiddle',
        // 添加中转
        meta: { title: 'addMiddle',  roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: ''},
        hidden: true,
      },
      {
        path: '/stock/addTransfer',
        component: () => import('@/views/transferWork/addTransfer'),
        name: 'addTransfer',
        // 添加调货
        meta: { title: 'addTransfer',  roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/stock/updatePacketGoods/:goodsId',
        component: () => import('@/views/packetGoods/updatePacketGoods'),
        name: 'updatePacketGoods',
        // 编辑小包商品
        meta: { title: 'updatePacketGoods',  roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/stock/createPacketGoods',
        component: () => import('@/views/packetGoods/createPacketGoods.vue'),
        name: 'createPacketGoods',
        // 上传小包商品
        meta: { title: 'createPacketGoods',  roles:['admin',ROLE_OPERATE,ROLE_INVENTORY_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },

    ]
  },
  // 贴标工单
  {
    path: '/workOrder',
    component: Layout,
    redirect: '/labelOrder/labelList',
    // 库存管理
    meta: {title: '工单管理', roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: 'circle'},
    children: [
      {
        path: '/workOrder/addWorkOrder',
        component: () => import('@/views/workOrder/addWorkOrder'),
        name: 'addWorkOrder',
        meta: { title: '添加工单',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: '', noCache: true},
      },
      {
        path: '/labelOrder/labelList',
        component: () => import('@/views/labelOrder/labelList'),
        name: 'labelList',
        // 贴标工单
        meta: { title: 'labelList',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: ''},
      },
      {
        path: '/labelOrder/labelInfo',
        component: () => import('@/views/labelOrder/labelOrderInfo'),
        name: 'labelOrderInfo',
        // 贴标详情
        meta: { title: 'labelInfo',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/stock/middleList',
        component: () => import('@/views/middleWork/middleList'),
        name: 'middleList',
        // 送仓工单
        meta: { title: 'middleList',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: ''},
      },
      {
        path: '/stock/middleInfo',
        component: () => import('@/views/middleWork/middleInfo'),
        name: 'middleInfo',
        // 送仓工单详情
        meta: { title: 'middleInfo',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/stock/transferList',
        component: () => import('@/views/transferWork/transferList'),
        name: 'transferList',
        // 调货工单
        meta: { title: 'transferList',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: ''},
      },
      {
        path: '/stock/transferInfo',
        component: () => import('@/views/transferWork/transferInfo'),
        name: 'transferInfo',
        // 调货工单详情
        meta: { title: 'transferInfo',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
      {
        path: '/combination/combinationList',
        component: () => import('@/views/combination/combinationList'),
        name: 'combinationList',
        // 组装工单
        meta: { title: 'combinationList',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: ''},
      },
      {
        path: '/combination/combinationInfo',
        component: () => import('@/views/combination/combinationInfo'),
        name: 'combinationInfo',
        // 组装工单详情
        meta: { title: 'combinationInfo',  roles: ['admin',ROLE_OPERATE,ROLE_WORKORDER_MANAGEMENT], icon: '', noCache: true},
        hidden: true,
      },
    ]
  },
  // 账号管理
  {
    path: '/user',
    component: Layout,
    redirect: '/user/accountList',
    // 账号管理
    meta: {title:'accountManagement',roles:['admin',ROLE_OPERATE,ROLE_ACCOUNT_MANAGEMENT], icon: 'circle'},
    children: [
       {
        //运营列表
        path: '/user/childList',
        component: () => import('@/views/user/childList'),
        name: 'childList',
        meta: { title: 'childList',  roles: ['admin']},
      },
      {
        path: '/user/accountList',
        component: () => import('@/views/user/accountList'),
        name: 'accountList',
        // 店铺列表
        meta: { title: 'storeList', roles:['admin',ROLE_OPERATE,ROLE_ACCOUNT_MANAGEMENT], icon: ''},
      },
      {
        path: '/business/notice',
        component: () => import('@/views/user/notice'),
        name: 'notice',
        meta: { title: 'notice', roles:['admin',ROLE_OPERATE]},
      },
      {
        path: '/user/rechargeLog',
        component: () => import('@/views/user/rechargeLog'),
        name: 'rechargeLog',
        // 充值记录
        meta: { title: 'amountLog', roles: ['admin',ROLE_OPERATE], icon: ''},
      },
      {
        path: '/user/resetPassword',
        component: () => import('@/views/user/resetPassword'),
        name: 'resetPassword',
        // 密码修改
        meta: { title: 'passwordModification', roles: ['admin',ROLE_OPERATE], icon: ''},
      },
      {
        path: '/user/userAddress',
        component: () => import('@/views/userAddress/userAddressList'),
        name: 'userAddressList',
        // 密码修改
        meta: { title: '地址管理', roles: ['admin',ROLE_OPERATE], icon: ''},
      },
    ]
  },
  // 系统设置
  {
    path: '/system',
    component: Layout,
    redirect: '/system/setting',
    // 系统设置
    meta: {title:'systemSetting',roles:['admin',ROLE_OPERATE], icon: 'circle'},
    children: [
      {
        path: '/system/setting',
        component: () => import('@/views/system/systemSetting'),
        name: 'systemSetting',
        meta: { title: 'systemSetting',  roles:['admin',ROLE_OPERATE], noCache: true},
      },
    ],
  },




  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/index', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
// 全局路由守卫,确保进入帮助中心是登录的状态

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}


export default router
